package org.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import org.dao.UserDAO;
import org.hibernate.Query;
import org.hibernate.Session;
import org.vo.User;

 public class UserDAOImpl implements UserDAO {
   private Session session;
   private PreparedStatement pstmt=null;
   public UserDAOImpl(Session session){
	   this.session=session;
   }
   @Override
	public boolean doCreate(User user) throws Exception {
		// TODO Auto-generated method stub
		this.session.save(user);
		return true;
	}

	@Override
	public List<User> findAll() throws Exception {
		// TODO Auto-generated method stub
		List<User> list=null;
		String hql="from User as user";
		Query query=this.session.createQuery(hql);
		list=query.list();
		return list;
	}

	@Override
	public User findById(int id) throws Exception {
		// TODO Auto-generated method stub
		User user2=null;
		String hql="from User as user where user.id=:id";
		Query query=this.session.createQuery(hql);
		query.setInteger("id", id);
		user2=(User) query.list().get(0);
		return user2;
	}
	@Override
	public boolean update(User user) throws Exception {
		// TODO Auto-generated method stub
		User user2=(User)session.get(User.class,user.getId());
		user2.setName(user.getName());
		user2.setSex(user.getSex());
		user2.setAge(user.getAge());
		user2.setMail(user.getMail());
		user2.setPasswd(user.getPasswd());
		session.update(user2);
		return true;
	}
      
}
